package com.wsoft.utils;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.RejectedExecutionHandler;

/**
 * @Author z001
 * @Date 2024/11/26 09:20
 */
public class ThreadPoolUtil {

    /**
     * 创建一个自定义的 ThreadPoolTaskExecutor
     *
     * @param corePoolSize      核心线程数
     * @param maxPoolSize       最大线程数
     * @param queueCapacity     工作队列容量
     * @param keepAliveSeconds  线程空闲时间（秒）
     * @param threadNamePrefix  线程名称前缀
     * @param allowCoreThreadTimeOut 是否允许核心线程超时
     * @param rejectedExecutionHandler 拒绝策略
     * @return 返回一个自定义的 ThreadPoolTaskExecutor
     */
    public static ThreadPoolTaskExecutor createThreadPoolTaskExecutor(
            int corePoolSize,
            int maxPoolSize,
            int queueCapacity,
            int keepAliveSeconds,
            String threadNamePrefix,
            boolean allowCoreThreadTimeOut,
            RejectedExecutionHandler rejectedExecutionHandler) {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
        executor.setRejectedExecutionHandler(rejectedExecutionHandler);
        executor.initialize();

        return executor;
    }


}
